package ru.nlezhnev.services.request_processor;

import java.sql.Connection;
import java.sql.SQLException;

import ru.nlezhnev.data_models.FarmDataModel;
import ru.nlezhnev.db.Database;
import ru.nlezhnev.utils.TypeUtils;
import flex.messaging.io.amf.ASObject;

public class AddFriends extends FarmRequestProcessor {
	public AddFriends(byte[] inputData, String vid, String methodName) {
		super(inputData, vid, methodName);
	}

	@SuppressWarnings("unchecked")
	protected Object performRequest() throws SQLException {
		Connection conn = Database.getInstance().getConnection();
		
		ASObject friendIds = (ASObject)data.get("fid");
		ASObject response = new ASObject();
		ASObject friends = new ASObject();
		response.put("friends_data", friends);		
		
		for (Object friendId : friendIds.values()) {
			FarmDataModel farm = new FarmDataModel(conn, TypeUtils.getInteger(friendId));
			friends.put(TypeUtils.getInteger(friendId), farm.getData().get("farm_data"));
		}
		
		conn.close();
		return prepareResponseData(response);
	}

}
